<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-table border row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange">
      <el-table-column label="设备名称" width="120" align="center" prop="equipmentName" />
      <el-table-column label="保养项" width="150" align="center" prop="itemName" />
      <el-table-column label="开始时间" align="center" prop="startTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="结束时间" align="center" prop="startTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="保养工时" align="center" prop="hours" width="180px" />
      <el-table-column label="责任人" width="100" align="center" prop="dutyUserName" />
      <el-table-column label="操作" align="center" fixed="right" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="openFormView('update', scope.row.waitId)">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <KeepTaskFormView ref="formViewRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { KeepTaskApi, KeepTask } from '@/api/business/keeptask'
import KeepTaskFormView from './KeepTaskFormView.vue'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'

/** 保养任务 列表 */
defineOptions({ name: 'KeepTask' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  equipmentId: undefined,
  startTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await KeepTaskApi.getKeepTaskPage(queryParams)
    list.value = data.list
    console.log("listlist", list.value);

    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}


const formViewRef = ref()
const openFormView = (type: string, id?: number) => {
  formViewRef.value.open(type, id)
}


const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: KeepTask[]) => {
  checkedIds.value = records.map((item) => item.id);
}
const name = ref()
//初始化数据
const initData = (row) => {
  console.log('子组件', row);
  name.value = row.dutyUserName
  queryParams.equipmentId = row.equipmentId
  getList()
}
defineExpose({ initData }) // 提供 open 方法，用于打开弹窗
/** 初始化 **/
onMounted(() => {
  // getList()
})
</script>
